<template>
  <el-container class='home-container'>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png">
        <span>后台管理系统</span>
      </div>
      <el-button type='info'
                 @click="logout">退出
      </el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧标栏菜单区域 -->
        <!-- unique-opened 是 :unique-opened="true"的简写  -->
        <!-- collapse 菜单折叠与否1折叠, 0不折叠 -->
        <!-- collapse-transition 取消折叠动画 -->
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409EFF"
                 unique-opened
                 router
                 :default-active="activePath"
                 :collapse-transition="false"
                 :collapse="isCollapse"
                  >
          <!-- 一级菜单 -->
          <!-- :index 后面只能跟字符串, 字符串和数字拼接, 得到一个字符串 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.icon"></i>
              <!-- 模板 -->
              <span>{{item.name}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
             :index="'/' + subItem.path"
             v-for="subItem in item.children"
             :key="subItem.id"
             @click="saveNavState('/' + subItem.path)"
             >
              <!-- 二级菜单的模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="subItem.icon"></i>
                <!-- 模板 -->
                <span>{{subItem.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // 是否折叠菜单, 默认不折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  // 生命周期函数, 创建实例完毕调用
  created () {
    // 调用getMenuList获取左侧所有菜单
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    // 保存链接的激活状态
    saveNavState (activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 点击按钮, 切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('admin/menus')
      if (res.code !== 200) return
      this.menuList = res.data
      // console.log(this.menuList)
    },
    logout () {
      // token清除
      sessionStorage.clear()
      // 跳转到登录
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  // flex布局
  display: flex;
  // 左右贴边对齐
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    margin-left: 10px;
    span {
      margin-left: 15px;
    }
    img {
      height: 50px;
    }
  }
}
.iconfont{
  margin-right: 8px;
  font-size: 18px;
  color: #fff;
}
.el-aside {
  background-color: #333744;
  // 去掉边框线, 使其导航菜单右侧对齐
  .el-menu{
    border-right:none
  }
}
.el-main {
  background-color: #eaedf1;
}
// 容器撑满全屏
.home-container {
  height: 100%;
}
.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color:#fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
